package homework;

import java.util.Scanner;

/**
 * @classname:Exception3
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/4 19:53
 * @version: 0.1
 * @since:1.8
 */
public class Exception3 {
    static int[] goods = {10, 10, 10, 10};
    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("========开始购物===========");
            System.out.println("请输入要购买商品序号");
            int index = input.nextInt();
            System.out.println("请输入要购买商品数量");
            int amount = input.nextInt();
            try {
                sellgoods(index, amount);
            } catch (GoodsBuyException g) {
                System.out.println(g.getMessage());
            }
            System.out.println("========结束购物===========");
        }
    }

    public static void sellgoods(int index, int amount) {
        if (index < 0 || index > goods.length) {
            throw new GoodsBuyException("没有该商品序号");
        }
        if (goods[index] == 0) {
            throw new GoodsBuyException("该商品存货为0");
        }

        if (goods[index] < amount) {
            throw new GoodsBuyException("该商品没有那么多存货");
        }
        System.out.println("购买成功");
        goods[index] -= amount;

    }
}
